package com.tikin.player.exo;


import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.analytics.AnalyticsListener;
import com.tikin.player.api.OnChangeListener;
import com.tikin.player.api.PlayerApi;

import java.util.List;

public class TikinExoMediaPlayer extends SurfaceView implements PlayerApi {

    protected ExoPlayer mPlayer;

    protected OnChangeListener mOnChangeListener;

    protected boolean isFirst = false;

    public TikinExoMediaPlayer(Context context) {
        super(context);
    }

    public TikinExoMediaPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TikinExoMediaPlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initPlayer() {
        isFirst = true;
        mPlayer = new ExoPlayer.Builder(getContext()).build();
        setPlayer(mPlayer);
        mPlayer.setRepeatMode(Player.REPEAT_MODE_ALL);
        AnalyticsListener mAnalyticsListener = new AnalyticsListener(){

        };
    }

    @Override
    public void addMediaItems(List<MediaItem> uris) {
        if (mPlayer == null){
            throw new IllegalArgumentException("onCreate初始化initPlayer()");
        }
        if ( uris == null || uris.isEmpty()) {
            return;
        }
        if (isFirst) {
            isFirst = false;
            mPlayer.addMediaItems(uris);
            mPlayer.prepare();
            startPlayer();
        } else {
            if (isPlaying()) {
                pausePlayer();
                mPlayer.clearMediaItems();
                mPlayer.addMediaItems(uris);
                mPlayer.seekTo(0, 0);
                startPlayer();
            }else {
                mPlayer.clearMediaItems();
                mPlayer.addMediaItems(uris);
                mPlayer.seekTo(0, 0);
            }
        }
        if (mOnChangeListener != null) {
            mOnChangeListener.onChange(uris.get(0));
        }
    }

    @Override
    public void startPlayer() {
        if (mPlayer != null) {
            mPlayer.setPlayWhenReady(true);
            mPlayer.getPlaybackState();
        }
    }

    @Override
    public void pausePlayer() {
        if (mPlayer != null) {
            mPlayer.setPlayWhenReady(false);
            mPlayer.getPlaybackState();
        }
    }

    @Override
    public void setOnChangeListener(OnChangeListener lis) {
        this.mOnChangeListener = lis;
    }

    @Override
    public boolean isPlaying() {
        return mPlayer.isPlaying();
    }

    @Override
    public void release() {
        if (mPlayer != null) {
            mPlayer.release();
        }
    }

    @Override
    public void seekToNextMediaItem() {
        if (mPlayer != null) {
            mPlayer.seekToNextMediaItem();
        }
    }

    @Override
    public void seekToPreviousMediaItem() {
        if (mPlayer != null) {
            mPlayer.seekToPreviousMediaItem();
        }
    }

    private void setPlayer(Player player) {
        if (player == null) {
            return;
        }
        player.setVideoSurfaceView(this);
        player.addListener(new Player.Listener() {
            @Override
            public void onPlaybackStateChanged(int playbackState) {
                switch (playbackState) {
                    case Player.STATE_ENDED:
                        Log.e("exo:state", playbackState + "");
                        break;
                    case Player.STATE_READY:
//                        Log.e("exo:state3", playbackState + "");
                        break;
                    case Player.STATE_BUFFERING:
//                        Log.e("exo:state2", playbackState + "");
                        break;
                    case Player.STATE_IDLE:
//                        Log.e("exo:state1", playbackState + "");
                        break;
                }
            }

            @Override
            public void onMediaItemTransition(MediaItem mediaItem, int reason) {
                if (mediaItem != null) {
//                    Log.e("exo:item", mediaItem.mediaId);
                    if (mOnChangeListener != null) {
                        mOnChangeListener.onChange(mediaItem);
                    }
                }
            }

        });
    }
}
